/**
 * Created by yangjiao on 2020/3/19.
 */
function getLinkPlan (store, data) {
  return store.dispatch('queryCustomerContactPlanHistory', data)
}
export default {
  data () {
    return {
      isChangeRight: false,
      isChangeTab: false,
      isSceen: false,
      loading: true,
      editTitle: '',
      initForm: {
        contactContent: '',
        region: '',
        executor: ''
      },
      isLoadMoreShow: false,
      linkPlan: [],
      accountId: '',
      userId: '',
      isShow: true,
      currentPlan: null,
      finishedPlan: null,
      title: this.$t('public.contactPlan'),
      isFinishedPlanShow: false,
      isEditShow: true,
      isCurrentPlanShow: false,
      editType: '',
      actionData: {
        customerId: '',
        page: 1,
        limit: 20
      }
    }
  },
  computed: {
    executorNew () {
      return this.currentPlan ? this.currentPlan.executor : ''
    },
    planContent () {
      return this.currentPlan ? this.currentPlan.remark : ''
    },
    planTime () {
      return this.currentPlan ? this.currentPlan.notifyTime : ''
    }
  },
  methods: {
    handleData (cid) {
      if (this.isSceen) {
        this.isSceen = false
        return
      }
      this.accountId = this.$store.state.session.user.account
      this.userId = this.$store.state.session.user._id
      if (this.refresh === 'plan' && cid) {
        this.actionData.customerId = cid
        this.loading = true
        this.editType = 'add'
        this.initForm = {
          contactContent: '',
          region: '',
          executor: ''
        }
        this.fetchData()
      }
    },
    // 联系计划时间格式化
    getFormatDateTime (date) {
      let year = date.getFullYear()
      let month = date.getMonth()
      let day = date.getDate()
      let hour = date.getHours()
      let minites = date.getMinutes()
      month = month + 1 > 9 ? month + 1 : '0' + (month + 1)
      day = day > 9 ? day : '0' + day
      hour = hour > 9 ? hour : '0' + hour
      minites = minites > 9 ? minites : '0' + minites
      return year + '-' + month + '-' + day + ' ' + hour + ':' + minites
    },
    // 获取联系计划历史数据
    fetchData () {
      // this.loading = true
      getLinkPlan(this.$store, this.actionData).then(res => {
        if (res.success) {
          // 当总条数小于20条 或者 请求是第一页 则初始化联系计划历史的数组为空 这样就可以
          // 连续点击卡片连续发起请求 请求还没回来 数组不断累加 出现数据重复的bug
          if (res.count < 20 || this.actionData.page === 1) {
            this.linkPlan = res.list || []
          } else {
            this.linkPlan = [...this.linkPlan, ...res.list]
          }
          // 大于20条 显示分页组件
          if (res.list && res.list.length < 20) {
            this.isLoadMoreShow = false
          } else {
            this.isLoadMoreShow = true
          }
          // 判断第一条数据联系计划为0的话 将是当前计划
          if (res.list.length > 0 && res.list[0].status === '0') {
            this.currentPlan = res.list[0]
            this.isEditShow = false
            this.isCurrentPlanShow = true
            this.isFinishedPlanShow = false
          } else {
            this.isEditShow = true
            this.isCurrentPlanShow = false
            this.isFinishedPlanShow = false
            this.isShow = true
            this.editTitle = this.$t('customer.contactPlan.newContactPlan')
          }
        }
        this.loading = false
      })
    },
    loadMore () {
      this.actionData.page++
      this.fetchData()
    },
    submitForm (formStacked) {
      let remark = formStacked.contactContent
      let time = formStacked.region
      let notifyTime = this.getFormatDateTime(time)
      let data = {
        accountId: this.accountId,
        userId: this.userId,
        executor: formStacked.executor || '',
        remark,
        notifyTime,
        customerId: this.cid
      }
//        if (remark.length > 140) {
//          this.$message.error(this.$t('public.notExceed'))
//        }
      this.$store.dispatch('addCustomerContactPlan', data).then(resp => {
        this.linkPlan = resp.list || []
        this.currentPlan = resp.list[0]
        this.isCurrentPlanShow = true
        this.isEditShow = false
        this.isFinishedPlanShow = false
      })
    },
    // 点击编辑按钮 执行相关操作
    edit () {
      this.isCurrentPlanShow = false
      this.isEditShow = true
      this.editType = 'edit'
      this.editTitle = this.$t('public.contactPlan')
    },
    // 更新联系计划
    update (formStacked) {
      let remark = formStacked.contactContent
      let time = formStacked.region
      let notifyTime = this.getFormatDateTime(time)
      let data = {
        _id: this.currentPlan._id,
        accountId: this.accountId,
        userId: this.userId,
        executor: formStacked.executor || '',
        remark,
        notifyTime,
        customerId: this.cid
      }
//        if (remark.length > 140) {
//          this.$message.error(this.$t('public.notExceed'))
//          return
//        }
      this.$store.dispatch('updateCustomerContactPlan', data).then(resp => {
        this.linkPlan = this.linkPlan.map(plan => {
          if (plan._id === data._id) {
            plan.remark = data.remark
            plan.notifyTime = data.notifyTime
            plan.executor = data.executor
          }
          return plan
        })
        this.currentPlan = this.linkPlan[0]
        this.isCurrentPlanShow = true
        this.isEditShow = false
      })
    },
    deal () {
      let data = {
        _id: this.cid,
        actionId: this.currentPlan._id,
        notifyTime: '',
        action: '',
        status: '1',
        userId: this.userId,
        executor: this.executorNew
      }
      this.$store.dispatch('dealCustomerContactPlan', data).then(resp => {
        this.linkPlan = resp.list || []
        this.isFinishedPlanShow = true
        this.isEditShow = true
        this.isCurrentPlanShow = false
        this.editType = 'add'
        this.editTitle = this.$t('customer.contactPlan.nextContactPlan')
      })
    },
    cancelDeal () {
      let data = {
        _id: this.cid,
        actionId: this.currentPlan._id,
        notifyTime: this.planTime,
        action: this.planContent,
        status: '0',
        userId: this.userId,
        executor: this.executorNew
      }
      this.$store.dispatch('dealCustomerContactPlan', data).then(resp => {
        this.linkPlan = resp.list || []
        this.isFinishedPlanShow = false
        this.isEditShow = false
        this.isCurrentPlanShow = true
      })
    }
  },
  props: {
    cid: String,
    refresh: String,
    refreshBusRight: Number
  },
  watch: {
    editType (cur, old) {
      this.initForm = cur === 'add' ? {
        contactContent: '',
        region: '',
        executor: ''
      } : {
        contactContent: this.planContent,
        region: new Date(this.planTime),
        executor: this.executorNew || ''
      }
    },
    // 客户id不变 没有点击卡片  只点击功能tab切换 比如操作日志到联系计划 刷新日志请求
    refresh (cur, old) {
      if (cur === 'plan') {
        if (this.isChangeTab) {
          this.isChangeTab = false
          return
        }
        this.isChangeRight = true
        this.actionData.customerId = this.cid
        this.actionData.page = 1
        this.editType = 'add'
        this.initForm = {
          contactContent: '',
          region: '',
          executor: ''
        }
        this.linkPlan = []
        if (this.cid) {
          this.fetchData()
        }
      }
    },
    //  当cid变化客户信息变化  联系计划模块初始化
    // 表单 和 历史模块初始化
    refreshBusRight (cur, old) {
      if (cur && this.refresh === 'plan') {
        // 如果refresh走啦 refreshBusRight不再调用fetchData
        if (this.isChangeRight) {
          this.isChangeRight = false
          return
        }
        this.isChangeTab = true
        this.actionData.customerId = this.cid
        this.actionData.page = 1
        this.linkPlan = []
        this.editType = 'add'
        this.initForm = {
          contactContent: '',
          region: '',
          executor: ''
        }
        if (this.cid) {
          this.fetchData()
        }
      }
    }
  },
  beforeMount () {
    this.isSceen = true
    this.accountId = this.$store.state.session.user.account
    this.userId = this.$store.state.session.user._id
    this.$nextTick(() => {
      if (this.refresh === 'plan' && this.cid) {
        this.actionData.customerId = this.cid
        this.loading = true
        this.editType = 'add'
        this.initForm = {
          contactContent: '',
          region: '',
          executor: ''
        }
        this.fetchData()
      }
    })
  }
}
